package com.graduation.mybatis.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.graduation.mybatis.bean.Food;
import com.graduation.mybatis.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Food)表控制层
 *
 * @author makejava
 * @since 2025-03-29 21:11:22
 */
@RestController
@RequestMapping("food")
public class FoodController {
    /**
     * 服务对象
     */
    @Autowired
    private FoodService foodService;


    /**
     * 分页查询用户
     *
     * @param currentPage 当前页码
     * @param pageSize    每页显示条数
     * @return 分页结果
     */
    @GetMapping("/page")
    public IPage<Food> getFoodPage(@RequestParam(defaultValue = "1") int currentPage,
                                   @RequestParam(defaultValue = "10") int pageSize) {
        return foodService.getFoodPage(currentPage, pageSize);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Map<String, Object>> queryById(@PathVariable Integer id) {
        try {
            Food food = foodService.getById(id);

            if (food == null) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Map.of(
                        "success", false,
                        "message", "未找到对应分类的食物"
                ));
            }

            return ResponseEntity.ok(Map.of(
                    "success", true,
                    "data", food
            ));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of(
                    "success", false,
                    "message", "服务器内部错误: " + e.getMessage()
            ));
        }
    }

    /**
     * 新增数据
     *
     * @param food 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Food> add(Food food) {
        return foodService.save(food) ?
                ResponseEntity.ok(food) :
                ResponseEntity.internalServerError().build();
    }

    /**
     * 编辑数据
     *
     * @param food 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Food> edit(Food food) {
        return foodService.updateById(food) ?
                ResponseEntity.ok(food) :
                ResponseEntity.internalServerError().build();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(@PathVariable Integer id) {
        return ResponseEntity.ok(this.foodService.removeById(id));
    }

    /**
     * 根据分类查询食物列表
     */
    @GetMapping
    public ResponseEntity<?> getFoodsByCategory(@RequestParam String category) {
        try {
            List<Food> foods = foodService.queryByCategory(category);

            if (foods == null || foods.isEmpty()) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Map.of(
                        "success", false,
                        "message", "未找到对应分类的食物"
                ));
            }

            return ResponseEntity.ok(Map.of(
                    "success", true,
                    "data", foods
            ));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of(
                    "success", false,
                    "message", "服务器内部错误: " + e.getMessage()
            ));
        }
    }

    @GetMapping("search")
    public Map<String, Object> searchFoods(@RequestParam String keyword) {
        Map<String, Object> response = new HashMap<>();
        try {
            List<Food> foods = foodService.searchFoods(keyword);
            response.put("success", true);
            response.put("data", foods);
        } catch (IllegalArgumentException e) {
            response.put("success", false);
            response.put("message", e.getMessage());
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "服务器内部错误");
        }
        return response;
    }
}

